<?php

namespace App\Console\Commands;

use App\Models\Chapter;
use App\Models\Novel;
use App\Models\Tag;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class PullAllNovelsFormOldLwa extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'pull:lwa-novels';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移原有 LWA 数据';

    protected $lwaDbClient;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->lwaDbClient = DB::connection('mysql-old-lwa');
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->setTag();
        $this->pullNovel();
        return 0;
    }

    private function setTag()
    {
        $this->lwaDbClient->table('tag')->get()->each(function ($row) {
            Tag::query()->firstOrCreate([
                'name' => $row->tag_name,
            ], [
                'name' => $row->tag_name,
            ]);
        });
        $this->line('Tag Set Over');
    }

    private function pullNovel()
    {
        $this->lwaDbClient->table('novels')->get()->each(function ($novel) {
            $novelExist = Novel::query()->where('novel_name', $novel->novel_name)->exists();
            if ($novelExist) {
                $this->line(sprintf('novel %s already pull over', $novel->novel_name));
                return;
            }
            $newNovel = Novel::query()->create([
                'novel_name' => $novel->novel_name,
                'tag_name' => $novel->tag_name,
                'intro' => $novel->intro,
                'is_done' => $novel->is_done,
                'words_num' => $novel->words_num,
                'chapter_num' => $novel->chapter_num,
                'is_hide' => $novel->hide,
                'cover_url' => $novel->cover_url,
                'coupon' => $novel->coupon,
                'author_name' => $novel->author_name,
                'author_id' => 0,
                'tag_id' => (function () use ($novel) {
                    $tag = Tag::query()->where('name', $novel->tag_name)->first();
                    if (! is_null($tag)) {
                        return $tag->id;
                    }
                    return 0;
                })(),
            ]);
            $this->line(sprintf('novel %s create success, start pull chapters...', $novel->novel_name));
            $this->pullChapter($novel->novel_id, $newNovel->id, $newNovel->novel_name);
        });
    }

    private function pullChapter($oldNovelId, $newNovelId, $novelName)
    {
        $this->lwaDbClient->table('chapter')->where('novel_id', $oldNovelId)->get()->each(function ($chapter) use ($newNovelId, $novelName) {
            $chapterExist = Chapter::query()->where('novel_id', $newNovelId)
                ->where('chapter_name', $chapter->chapter_name)->exists();

            if ($chapterExist) {
                $this->line(sprintf('novel %s, chapter %s already pull', $novelName, $chapter->chapter_name));
                return;
            }
            $newChapter = Chapter::query()->create([
                'chapter_name' => $chapter->chapter_name,
                'novel_id' => $newNovelId,
                'order_num' => $chapter->order_num,
                'word_num' => $chapter->word_num,
                'is_vip' => $chapter->is_vip,
                'coupon' => $chapter->coupon,
                'content' => $chapter->content,
                'publish' => $chapter->publish,
            ]);
            $this->line(sprintf('novel %s chapter %s create', $novelName, $newChapter->chapter_name));
        });
    }
}
